﻿using System.Reflection.Metadata;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Mysqlx.Crud;
using NPOI.XSSF.UserModel;
using Production_Get.API.Application.Commands;
using Production_Get.API.Application.MaterialManagement;
using Production_Get.API.Application.MaterialManagement.Mang;
using Production_Get.API.Application.MaterialManagement.MaterialRequirementsPlanning;
using Production_Get.API.Application.MaterialManagement.Row;
using Production_Get.API.Application.MaterialManagement.Select;
using THE_WIND_RISES.Common.Domans.BasicInformation.Departments;
using THE_WIND_RISES.Common.Domans.OutboundOrder;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Controllers.MaterialManagement
{
    [Route("api/[controller]")]
    [ApiController]
    public class ShowMaterialController : ControllerBase
    {
        private readonly IMediator mediator;
        /// <summary>
        /// 中介者
        /// </summary>
        /// <param name="mediator"></param>
        public ShowMaterialController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 物料/服务下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetMaterial")]
        public async Task<Result> GetMaterial()
        {
            return await mediator.Send(new MaterialSeachCommon());
        }
        /// <summary>
        /// 物料单位下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetItemUnit")]
        public async Task<Result> GetItemUnit(ItemUnitSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料类别下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetItemType")]
        public async Task<Result> GetItemType()
        {
            return await mediator.Send(new ItemTypeSeachCommon());
        }
        /// <summary>
        /// 规格单位下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetSpecsUnit")]
        public async Task<Result> GetSpecsUnit(SpecsUnitSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 规格下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetSpecs")]
        public async Task<Result> GetSpecs(SpecScretenComm cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料选择下拉框
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetSelection")]
        public async Task<Result> GetSelection()
        {
            return await mediator.Send(new SelectionSeachCommon());
        }

        /// <summary>
        /// 物料属性反填
        /// </summary>
        /// <param name="com"></param>
        /// <returns></returns>
        [HttpPost("GetAttributeRow")]
        public async Task<Result> GetAttributeRow(AttributeRowScretenComm com)
        {
            return await mediator.Send(com);
        }
        /// <summary>
        /// 物料信息显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetItemInfo")]
        public async Task<Result> GetItemInfo(ItemInfoScretenCommend cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料清单
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetItemManifestInfo")]
        public async Task<Result> GetItemManifestInfo(ItemManifestInfoSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料需求计划显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetMaterialPlanning")]
        public async Task<Result> GetMaterialPlanning(MaterialPlanningSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料需求计划返填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetMaterialPlan")]
        public async Task<Result> GetMaterialPlan(MaterialPlanningMangCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 导出物料需求计划
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("ExportMaterialPlanning")]
        public async Task<IActionResult> GetMaterialPlanning()
        {
            try
            {
                //获取需要导出的数据
                Result list = await mediator.Send(new MaterialPlanningSeachCommon());
                //转换数据类型
                List<MaterialPlanning> ExcelData = list.Data as List<MaterialPlanning>;
                //实例化一个Excel表格
                var WB = new XSSFWorkbook();
                var sheet = WB.CreateSheet("物料需求计划表");
                var row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("计划编码");
                row.CreateCell(2).SetCellValue("计划名称");
                row.CreateCell(3).SetCellValue("计划描述");
                row.CreateCell(4).SetCellValue("状态");
                row.CreateCell(5).SetCellValue("制单人");
                row.CreateCell(6).SetCellValue("制单部门");
                row.CreateCell(7).SetCellValue("创建人");
                row.CreateCell(8).SetCellValue("创建内容");
                row.CreateCell(9).SetCellValue("创建时间");
                row.CreateCell(10).SetCellValue("审核状态");
                row.CreateCell(11).SetCellValue("是否删除");
                int i = 0;
                foreach (MaterialPlanning item in ExcelData)
                {
                    i++;
                    var rows = sheet.CreateRow(i);
                    rows.CreateCell(0).SetCellValue(item.MaterialPlanningId);
                    rows.CreateCell(1).SetCellValue(item.PlanningCode);
                    rows.CreateCell(2).SetCellValue(item.PlanningName);
                    rows.CreateCell(3).SetCellValue(item.PlanningDesc);
                    rows.CreateCell(4).SetCellValue(item.PlanningState == false ? "创建" : (item.PlanningState == false ? "完成" : "删除"));
                    rows.CreateCell(5).SetCellValue(item.PlanningPeole);
                    rows.CreateCell(6).SetCellValue(item.DepartmentId == 1 ? "JAVA组" : (item.DepartmentId == 2 ? "JAVA2组" : "JAVA3组"));
                    rows.CreateCell(7).SetCellValue(item.Handle);
                    rows.CreateCell(8).SetCellValue(item.HandleContext);
                    rows.CreateCell(9).SetCellValue(item.HandleTime.ToString());
                    rows.CreateCell(10).SetCellValue(item.Pid == 1 ? "创建" : (item.Pid == 2 ? "审批" : "删除"));
                    rows.CreateCell(11).SetCellValue(item.IsDelete);
                }
                byte[] bytes;
                using (var ms = new MemoryStream())
                {
                    WB.Write(ms);
                    bytes = ms.ToArray();
                }
                var main = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bytes, main,"物料需求计划表.xlsx");
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        /// <summary>
        /// 物料清单行
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetRowBillMateria")]
        public async Task<Result> GetRowBillMateria(RowBillMateriaSeachCommon cmd)
        {
            
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料需求行
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetRowMaterialPlanning")]
        public async Task<Result> GetRowMaterialPlanning(RowMaterialPlanningSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 物料清单返填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("MangItemManifestInfo")]
        public async Task<Result> MangItemManifestInfo(ItemManifestInfoMangCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 工艺路线返填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("MangProcessRouteMangCommon")]
        public async Task<Result> MangProcessRouteMangCommon(ProcessRouteMangCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 工艺路线行显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetRowTask")]
        public async Task<Result> GetRowTask(RowTaskSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 工艺路线显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetTaskSeach")]
        public async Task<Result> GetTaskSeach(TaskSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 生产任务反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("TaskMang")]
        public async Task<Result> TaskMang(TaskMangCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 出库单显示
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("GetOutBound")]
        public async Task<Result> GetOutBound(OutBoundSeachCommon cmd)
        {
            return await mediator.Send(cmd);
        }
        /// <summary>
        /// 导出出库单
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("GetOutBoundExcel")]
        public async Task<IActionResult> GetOutBoundExcel()
        {
            try
            {
                //获取需要导出的数据
                Result list = await mediator.Send(new OutBoundSeachCommon());
                //转换数据类型
                List<OutBound> ExcelData = list.Data as List<OutBound>;
                //实例化一个Excel表格
                var WB = new XSSFWorkbook();
                var sheet = WB.CreateSheet("出库单");
                var row = sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("序号");
                row.CreateCell(1).SetCellValue("单据编码");
                row.CreateCell(2).SetCellValue("来源");
                row.CreateCell(3).SetCellValue("来源编码");
                row.CreateCell(4).SetCellValue("出库类型");
                row.CreateCell(5).SetCellValue("仓库");
                row.CreateCell(6).SetCellValue("出库时间");

                row.CreateCell(7).SetCellValue("制单人");
                row.CreateCell(8).SetCellValue("制单部门");
                row.CreateCell(9).SetCellValue("创建时间");
                row.CreateCell(10).SetCellValue("状态");
                row.CreateCell(11).SetCellValue("是否删除");
                int i = 0;
                foreach (OutBound item in ExcelData)
                {
                    i++;
                    var rows = sheet.CreateRow(i);
                    rows.CreateCell(0).SetCellValue(item.BoundId);
                    rows.CreateCell(1).SetCellValue(item.BoundNo);
                    rows.CreateCell(2).SetCellValue(item.BoundSource);
                    rows.CreateCell(3).SetCellValue(item.SourceNo);
                    rows.CreateCell(4).SetCellValue(item.BoundType == 1 ? "创建" : (item.BoundType == 2 ? "完成" : "删除"));
                    rows.CreateCell(5).SetCellValue(item.WarehouseId);
                    rows.CreateCell(6).SetCellValue(item.BoundDate.ToString());
                    rows.CreateCell(7).SetCellValue(item.BoundPeople);
                    rows.CreateCell(8).SetCellValue(item.BoundDepartment);
                    rows.CreateCell(9).SetCellValue(item.HandleTime.ToString());
                    rows.CreateCell(10).SetCellValue(item.Pid == 1 ? "创建" : (item.Pid == 2 ? "审批" : "删除"));
                    rows.CreateCell(11).SetCellValue(item.IsDelete);
                }
                byte[] bytes;
                using (var ms = new MemoryStream())
                {
                    WB.Write(ms);
                    bytes = ms.ToArray();
                }
                var main = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(bytes, main, "出库单.xlsx");
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 出库单反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("OutBoundMang")]
        public async Task<Result> utBoundMang(OutBoundMangCommon cmd)
        {
            return await mediator.Send(cmd);
        }
    }
}
